package org.zjx.util.clickprecision;

import java.awt.Point;

/**
 * 像素坐标系核心 - 处理游戏坐标与屏幕坐标的转换
 * 所有计算都在像素坐标系中完成，包括阻挡区域检测
 */
public class PixelCoordinateSystem {
    private final int screenWidth;
    private final int screenHeight;
    private final int pixelsPerUnit;
    private final int pixelMapWidth;
    private final int pixelMapHeight;
    private final Point screenCenter;

    public PixelCoordinateSystem(int screenWidth, int screenHeight, int pixelsPerUnit,
                                 int gameMapWidth, int gameMapHeight) {
        this.screenWidth = screenWidth;
        this.screenHeight = screenHeight;
        this.pixelsPerUnit = pixelsPerUnit;
        this.pixelMapWidth = gameMapWidth * pixelsPerUnit;
        this.pixelMapHeight = gameMapHeight * pixelsPerUnit;
        this.screenCenter = new Point(screenWidth / 2, screenHeight / 2);
    }

    /**
     * 游戏坐标转像素地图坐标
     */
    public Point gameToPixelMap(Point gamePos) {
        return new Point(gamePos.x * pixelsPerUnit, gamePos.y * pixelsPerUnit);
    }

    /**
     * 计算屏幕中心在像素地图中的位置（考虑边界）
     */
    public Point calculateScreenCenterInPixelMap(Point playerGamePos) {
        Point playerPixel = gameToPixelMap(playerGamePos);

        int centerPixelX = calculateBoundedCenterX(playerPixel.x);
        int centerPixelY = calculateBoundedCenterY(playerPixel.y);

        return new Point(centerPixelX, centerPixelY);
    }

    private int calculateBoundedCenterX(int playerPixelX) {
        if (playerPixelX < screenCenter.x) return screenCenter.x;
        return Math.min(playerPixelX, pixelMapWidth - screenCenter.x);
    }

    private int calculateBoundedCenterY(int playerPixelY) {
        if (playerPixelY < screenCenter.y) return screenCenter.y;
        return Math.min(playerPixelY, pixelMapHeight - screenCenter.y);
    }

    /**
     * 计算目标点在屏幕上的点击位置（纯像素计算）
     */
    public Point calculateScreenClickPosition(Point playerGamePos, Point targetGamePos) {
        Point targetPixel = gameToPixelMap(targetGamePos);
        Point screenCenterPixel = calculateScreenCenterInPixelMap(playerGamePos);

        Point relativeVector = new Point(
                targetPixel.x - screenCenterPixel.x,
                targetPixel.y - screenCenterPixel.y
        );

        int screenX = screenCenter.x + relativeVector.x;
        int screenY = screenCenter.y - relativeVector.y;

        return new Point(screenX, screenY);
    }

    public Point calculateScreenClickPositionByCenterPos(Point screenCenterPos, Point targetGamePos) {

        Point relativeVector = new Point(
                targetGamePos.x - screenCenterPos.x,
                targetGamePos.y - screenCenterPos.y
        );

        int screenX = screenCenter.x + relativeVector.x * pixelsPerUnit;
        int screenY = screenCenter.y - relativeVector.y * pixelsPerUnit;

        return new Point(screenX, screenY);
    }

    public Point getScreenCenter() {
        return new Point(screenCenter);
    }

    public boolean isScreenPositionValid(Point screenPos) {
        return screenPos.x >= 0 && screenPos.x <= screenWidth &&
                screenPos.y >= 0 && screenPos.y <= screenHeight;
    }
}